package com.css.zfzx.fwsjcj.modules.structure.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fwsjcj.commen.util.DownloadUtils;
import com.css.zfzx.fwsjcj.modules.structure.entity.FwBrickStructureEntity;
import com.css.zfzx.fwsjcj.modules.structure.entity.FwBrickwoodStructureEntity;
import com.css.zfzx.fwsjcj.modules.structure.service.FwBrickStructureService;
import com.css.zfzx.fwsjcj.modules.structure.service.FwBrickwoodStructureService;
import com.css.zfzx.fwsjcj.modules.structure.vo.StructureQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author leon
 * @Title: FwBrickwoodStructureController
 * @Description: 砖木结构
 * @date 2020/6/3 10:03
 */
@RestController
@RequestMapping("/fw/brickwood")
@Slf4j
public class FwBrickwoodStructureController {
    @Autowired
    private FwBrickwoodStructureService brickwoodStructureService;
    @Autowired
    DownloadUtils downloadUtils;

    /**
     * 增加
     *
     * @param fwBrickwoodStructureEntity
     * @return
     */
    @PostMapping("save")
    public RestResponse save(@RequestBody FwBrickwoodStructureEntity fwBrickwoodStructureEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            brickwoodStructureService.save(fwBrickwoodStructureEntity);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param fwBrickwoodStructureEntity
     * @return
     */
    @PostMapping("update")
    public RestResponse update(@RequestBody FwBrickwoodStructureEntity fwBrickwoodStructureEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            brickwoodStructureService.update(fwBrickwoodStructureEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导入砖木结构
     *
     * @param file
     * @param response
     * @return
     */
    @PostMapping("export/{taskName}/{taskId}")
    public RestResponse export(@PathVariable("taskName") String taskName, @PathVariable("taskId") String taskId, @RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = brickwoodStructureService.exportExcel(taskName, taskId, file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
